
package javaproject;

import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

public class SpinGroup extends TransformGroup{
    public double radX = 0; //角度範圍[0, 2*PI)
    public double radY = 0;
    public double radZ = 0;
    public Transform3D T = new Transform3D();
    public Transform3D Tx = new Transform3D();
    public Transform3D Ty = new Transform3D();
    public Transform3D Tz = new Transform3D();

    public void rotX(double x)
    {
        if(x+radX >= Math.PI*2)
            radX = x+radX - Math.PI*2;
        else if(x+radX < 0)
            radX = x+radX + Math.PI*2;
        else
            radX += x;
	T.rotX(x);
        Tx.mul(T);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotY(double y)
    {
        if(y+radY >= Math.PI*2)
            radY = y+radY - Math.PI*2;
        else if(y+radY < 0)
            radY = y+radY + Math.PI*2;
        else
            radY += y;
	T.rotY(y);
        Ty.mul(T);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotZ(double z)
    {
        if(z+radZ >= Math.PI*2)
            radZ = z+radZ - Math.PI*2;
        else if(z+radZ < 0)
            radZ = z+radZ + Math.PI*2;
        else
            radZ += z;
	T.rotZ(z);
        Tz.mul(T);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotXto(double x)
    {
        if(x >= Math.PI*2)
            radX = x - Math.PI*2;
        else if(x < 0)
            radX = x + Math.PI*2;
        else
            radX = x;
        Tx.rotX(radX);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotYto(double y)
    {
        if(y >= Math.PI*2)
            radY = y - Math.PI*2;
        else if(y < 0)
            radY = y + Math.PI*2;
        else
            radY = y;
        Ty.rotY(radY);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotZto(double z)
    {
        if(z >= Math.PI*2)
            radZ = z - Math.PI*2;
        else if(z < 0)
            radZ = z + Math.PI*2;
        else
            radZ = z;
        Tz.rotZ(radZ);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
    public void rotTo(double x, double y, double z)
    {
        if(x >= Math.PI*2)
            radX = x - Math.PI*2;
        else if(x < 0)
            radX = x + Math.PI*2;
        else
            radX = x;
        if(y >= Math.PI*2)
            radY = y - Math.PI*2;
        else if(y < 0)
            radY = y + Math.PI*2;
        else
            radY = y;
	if(z >= Math.PI*2)
            radZ = z - Math.PI*2;
        else if(z < 0)
            radZ = z + Math.PI*2;
        else
            radZ = z;

	Tx.rotX(radX);
	Ty.rotY(radY);
        Tz.rotZ(radZ);
        T.set(Tx);
        T.mul(Ty);
        T.mul(Tz);
        setTransform(T);
    }
}